package com.lin.missyou.repository;

import com.lin.missyou.models.Order;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;
import java.util.Optional;

@Repository
public interface OrderRepository extends JpaRepository<Order, Long> {
    Page<Order> findByExpiredTimeGreaterThanAndUserIdAndStatus(Date now, Long uid, Integer status, Pageable pageable);

    Page<Order> findByUserId(Long uid, Pageable pageable);

    Page<Order> findByUserIdAndStatus(Long uid,Integer status,Pageable pageable);


    Optional<Order> findFirstByIdAndUserId(Long oid,Long uid);

    Optional<Order> findFirstByOrderNo(String orderNo);

    @Modifying
    @Query("update Order o set o.status =:status where o.orderNo=:orderNo")
    int updateStatusByOrderNo(String orderNo,Integer status);

    @Modifying
    @Query("update Order o set o.status = 5 where o.status=1 and o.id = :oid")
    int cancelOrder(Long oid);
}
